﻿<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Dos.ORM" %>
<%@ Import Namespace="Dos.Model" %>

<!DOCTYPE html>

<script runat="server">
    public string keys = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        List<lpsos_key> data = DB.Context.From<lpsos_key>().Select(k => new { k.Name }).ToList();
        foreach (lpsos_key item in data)
            keys += item.Name + ",";
        keys = keys.Substring(0, keys.Length - 2);
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        /*article { display: none; }*/
    </style>
</head>
<body>
    <article>
    </article>
    <aside class="logs"></aside>

    <script src="content/js/jquery.js"></script>
    <script>
        var keys = '<%=keys %>'.split(','),
            link = 'http://search.ci123.com';


        //var res = getInfo('http://www.ci123.com/article.php/74593');
        //var d = $(res).find('.info');
        //d.find('.for-more').remove();
        //$('article').append(d.html());


        $(function () {

            var html,
                page, now = 0,
                count = -1;
            count = getcount('湿疹');
            var tmp = setInterval(function () {

                build('湿疹', now);
                now++;
                if (now >= count) clearInterval(tmp);
            }, 2000);
        });

        function build(k, p) {
            var idx = 0;
            $.ajax({
                type: 'post', url: '/handle/Resoure.ashx', data: { url: link, key: k, p: p }, async: true, dataType: 'HTML',
                success: function (data, status) {
                    html = $(data).find("#left > .text");
                    getdata(html, k);
                    $('.logs').append($('<p>关键字：' + k + ', 页数 [' + p + '] 抓取完成。</p>'));
                    console.log(ci.data);

                }
            });
        }

        function getcount(k) {
            var count = -1;
            $.ajax({
                type: 'post', url: '/handle/Resoure.ashx', data: { url: link, key: k, p: 1, tmp: new Date().getTime() }, async: false, dataType: 'HTML',
                success: function (data, status) {
                    count = $(data).find(".last").attr("href").split('?')[1].split('=')[1];
                }
            });
            return count;
        }

        function getdata(list, key) {

            list.each(function () {
                var o = $(this),
                    e = ci.entity();
                e.Key = key;
                e.Title = o.find('h1 > a').attr('title');
                e.Link = o.find('h1 > a').attr('href');
                var txt = o.find('p').text().replace('来源：', '').split(' - ');
                e.Category = txt[0];
                if (txt.length > 1) e.Publishdate = txt[1].replace('年', '.').replace('月', '.').replace('日', '');
                o.children('h1').remove();
                o.children('p').remove();
                e.Summary = o.text().trim();
                var res;

                if (e.category.indexOf('育儿百科') > -1) {
                    res = getInfo(e.link);
                    e.Info = $(res).find('.bottom').html();
                } else if (e.category.indexOf('父母学堂') > -1) {
                    res = getInfo(e.link);
                    var t = $(res).find('.info');
                    t.find('.for-more').remove();
                    e.Info = t;
                } else if (e.category.indexOf('育儿论坛') > -1) {
                    res = getInfo(e.link);
                    e.Info = $(res).find('#text_1_0').html();
                }

                if (e.Publishdate >= '2016.6.1' && e.Publishdate <= '2017.5.31') {
                    ci.data.push(e);
                } else {
                    if (e.Publishdate == "") ci.data.push(e);
                }
                $.ajax({
                    type: 'post', url: '/handle/Insert.ashx', data: { source: e }, async: false, dataType: 'JSON',
                    success: function (data, status) {
                        res = data
                    }
                });
            });
        }

        function getInfo(url) {
            var res;
            $.ajax({
                type: 'post', url: '/handle/GetInfo.ashx', data: { url: url }, async: false, dataType: 'HTML',
                success: function (data, status) {
                    res = data
                }
            });
            return res;
        }

        var ci = CI213();
        function CI213() {
            var obj = {};
            // 数据集合
            obj.data = [];
            // 实体对象
            obj.entity = function () {
                var e = { Title: '', Summary: '', Link: '', Info: '', PublishDate: '', Category: '', Author: '', Authorlink: '', Key: '' };
                return e;
            }
            return obj;
        }



    </script>
</body>
</html>
